import { useState } from "@/hooks";
import { defineStore } from "pinia";
import { ref, computed } from "vue";

export const useAppStore = defineStore("app", () => {
  /** 用户不是第一次使用(状态值)钩子 */
  const userIsNotFirstUseHook = useState<{
    notFirstUse: boolean;
  }>(
    () => ({
      notFirstUse: false,
    }),
    {
      moduleKey: "USER_IS_NOT_FIRST_USE",
      debug: true,
      cache: true,
    },
  );

  /** 是第一次使用 */
  const isFirstUse = computed({
    get() {
      return !userIsNotFirstUseHook.state.notFirstUse;
    },
    set(val: boolean) {
      userIsNotFirstUseHook.updateState({
        notFirstUse: !val,
      });
    },
  });

  /** 刚启动 */
  const isLaunchJustNow = ref(true);

  const showGuide = computed(() => isLaunchJustNow.value || isFirstUse.value);

  /** 更新第一次使用 */
  const changeIsFirstUse = (status = false) => {
    isFirstUse.value = status;
  };

  let timer: number;

  /** 消费启动值 */
  const consumeLaunchJustNow = () => {
    if (timer) {
      console.log(`[useAppStore] 已经消费启动项`, timer);
      return;
    }
    timer = setTimeout(() => {
      isLaunchJustNow.value = false;
    }, 1000) as unknown as number;
  };

  return {
    isFirstUse,
    showGuide,
    changeIsFirstUse,
    consumeLaunchJustNow,
  };
});
